//
//  QuDanSquareViewCell.m
//  QuDanOEM
//
//  Created by mac on 2021/10/23.
//

#import "QuDanSquareViewCell.h"
#import "QuDanSearchNetWork.h"
#import "QuDanSquareNetWork.h"
@implementation QuDanSquareViewCell
- (void)awakeFromNib {
    [super awakeFromNib];
    self.iconImage.layer.cornerRadius = 45/2;
    self.iconImage.layer.masksToBounds = YES;
    self.attentionBtn.layer.cornerRadius = 2.0f;
    self.attentionBtn.layer.masksToBounds = YES;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];
}

- (void)setItemModel:(QuDanSquareRecordsItem *)itemModel
{
    _itemModel = itemModel;
    [self.iconImage sd_setImageWithURL:[NSURL URLWithString:itemModel.member.imgUrl] placeholderImage:[UIImage imageNamed:@"qd_test"]];
    self.nameLab.text = itemModel.member.nickName;
    self.contentLab.text = itemModel.title;
    self.timeLab.text = itemModel.addTime;
    [self.startBtn setTitle:[NSString stringWithFormat:@"%ld",itemModel.status] forState:UIControlStateNormal];
    [self.messageBtn setTitle:[NSString stringWithFormat:@"%ld",itemModel.comment] forState:UIControlStateNormal];
    if (itemModel.item.count == 0) {
        self.ImageContainer.hidden = YES;
    }else{
        self.ImageContainer.hidden = NO;
        if (itemModel.item.count == 1) {
            self.firstImg.hidden = NO;
            self.secondImg.hidden = YES;
            self.thirdImg.hidden = YES;
            [self.firstImg sd_setImageWithURL:[NSURL URLWithString:itemModel.item[0].url] placeholderImage:[UIImage imageNamed:@"qd_test"]];
        }else if(itemModel.item.count == 2){
            self.firstImg.hidden = NO;
            self.secondImg.hidden = NO;
            self.thirdImg.hidden = YES;
            [self.firstImg sd_setImageWithURL:[NSURL URLWithString:itemModel.item[0].url] placeholderImage:[UIImage imageNamed:@"qd_test"]];
            [self.firstImg sd_setImageWithURL:[NSURL URLWithString:itemModel.item[1].url] placeholderImage:[UIImage imageNamed:@"qd_test"]];
        }else{
            self.firstImg.hidden = NO;
            self.secondImg.hidden = NO;
            self.thirdImg.hidden = NO;
            [self.firstImg sd_setImageWithURL:[NSURL URLWithString:itemModel.item[0].url] placeholderImage:[UIImage imageNamed:@"qd_test"]];
            [self.secondImg sd_setImageWithURL:[NSURL URLWithString:itemModel.item[1].url] placeholderImage:[UIImage imageNamed:@"qd_test"]];
            [self.thirdImg sd_setImageWithURL:[NSURL URLWithString:itemModel.item[2].url] placeholderImage:[UIImage imageNamed:@"qd_test"]];
        }
    }
    if (itemModel.follow == YES) {
        //已关注
        self.attentionBtn.backgroundColor = [UIColor bm_colorGradientChangeWithSize:CGSizeMake(70, 35) direction:PageGradientChangeDirectionLevel startColor:PostColorP(0xD8D8D8,1) endColor:PostColorP(0xD8D8D8 ,1)];
        [self.attentionBtn setTitle:@"已关注" forState:UIControlStateNormal];
    }else{
        //没有关注
        self.attentionBtn.backgroundColor = [UIColor bm_colorGradientChangeWithSize:CGSizeMake(70, 35) direction:PageGradientChangeDirectionLevel startColor:PostColorP(0xFF5121,1) endColor:PostColorP(0xFFC021 ,1)];
        [self.attentionBtn setTitle:@"+关注" forState:UIControlStateNormal];
    }
}
/// 关注
/// @param sender 关注
- (IBAction)actionAttentionBtn:(UIButton *)sender
{
    if (self.itemModel.follow == YES) {
        //取消关注
        NSMutableDictionary *param = [NSMutableDictionary dictionary];
        param[@"mId"] = PostManage.shareInstance.userModel.ids;
        param[@"obj"] = @(self.itemModel.mid);
        param[@"type"] = @"0";
        [[QuDanSquareNetWork shareInstance]requestSquareFllowOrCancel:param viewController:nil andBlock:^(id  _Nullable data, BOOL result) {
            NSLog(@"取消关注.data:%@",data);
            self.itemModel.follow = NO;
            self.attentionBtn.backgroundColor = [UIColor bm_colorGradientChangeWithSize:CGSizeMake(70, 35) direction:PageGradientChangeDirectionLevel startColor:PostColorP(0xFF5121,1) endColor:PostColorP(0xFFC021 ,1)];
            [self.attentionBtn setTitle:@"+关注" forState:UIControlStateNormal];
        }];
    }else{
        //关注
        NSMutableDictionary *param = [NSMutableDictionary dictionary];
        param[@"mId"] = PostManage.shareInstance.userModel.ids;
        param[@"obj"] = @(self.itemModel.mid);
        param[@"type"] = @"1";
        NSLog(@"param:%@",param);
        [[QuDanSquareNetWork shareInstance]requestSquareFllowOrCancel:param viewController:nil andBlock:^(id  _Nullable data, BOOL result) {
            NSLog(@"关注用户.data:%@",data);
            self.itemModel.follow = YES;
            self.attentionBtn.backgroundColor = [UIColor bm_colorGradientChangeWithSize:CGSizeMake(70, 35) direction:PageGradientChangeDirectionLevel startColor:PostColorP(0xD8D8D8,1) endColor:PostColorP(0xD8D8D8 ,1)];
            [self.attentionBtn setTitle:@"已关注" forState:UIControlStateNormal];
        }];
    }
}
@end
